package com.ls.fw.token.support.csrf;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.servlet.support.RequestDataValueProcessor;

import com.ls.fw.token.handler.HttpTokenHandler;

/**
 * <bean name="requestDataValueProcessor" 
 * class="com.ls.fw.mvc.springmvc.support.csrf.CSRFRequestDataValueProcessor"/>
 * 
 * @author lisheng
 * @date 2016年2月28日 下午11:07:14
 * @version V1.0
 */
public class CSRFRequestDataValueProcessor implements RequestDataValueProcessor
{

	private HttpTokenHandler  httpTokenHandler;

	@Override
	public String processAction(HttpServletRequest request, String action,
			String httpMethod) {
		return action;
	}

	@Override
	public String processFormFieldValue(HttpServletRequest request,
			String name, String value, String type) {
		return value;
	}

	@Override
	public Map<String, String> getExtraHiddenFields(HttpServletRequest request) {
		Map<String, String> hiddenFields = new HashMap<String, String>();
		hiddenFields.put(httpTokenHandler.getTokenRepository().getParameterName(), httpTokenHandler.getToken(request, null, true));
		return hiddenFields;
	}

	@Override
	public String processUrl(HttpServletRequest request, String url) {
		return url;
	}

	public HttpTokenHandler getHttpTokenHandler() {
		return this.httpTokenHandler;
	}

	public void setHttpTokenHandler(HttpTokenHandler httpTokenHandler) {
		this.httpTokenHandler = httpTokenHandler;
	}
    
}